package com.example.final_project.Usr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.R;
import com.example.final_project.Usr.adapter.ExplanAdapter;
import com.example.final_project.Usr.entry.Explan;

import java.util.List;

public class ExplanActivity extends AppCompatActivity {

    private ImageView back;
    private RecyclerView text;
    private Context context;
    public static final int INIT = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_explan);

        findById();
        ClickManage();
        getData();

    }

    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case INIT:
                    List<Explan> explans = (List<Explan>) msg.obj;
                    ExplanAdapter explanAdapter = new ExplanAdapter(context,explans);
                    text.setAdapter(explanAdapter);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
                    text.setLayoutManager(linearLayoutManager);

            }


        }
    };

    public void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/explan")
                .bind(this)
                .setOnResList(Explan.class,explans ->{
                    Message message = Message.obtain();
                    message.what = INIT;
                    message.obj = explans;
                    handler.sendMessage(message);
                })
                .get();
    }

    public void findById(){
        back = findViewById(R.id.explan_back);
        text = findViewById(R.id.explan_text);
        context = this.getBaseContext();
    }

    public void ClickManage(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ExplanActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

    }

}